﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace API.Core.ActionResults
{
    public class CommerceMLResult : System.Web.Mvc.ActionResult
    {
        public IEnumerable<API.Models.V_IncomingInvoices> Invoices { get; set; }

        public CommerceMLResult() { }

        public CommerceMLResult(IEnumerable<API.Models.V_IncomingInvoices> invoices)
        {
            this.Invoices = invoices;
        }

        public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
        {
            HttpContextBase httpContextBase = context.HttpContext;
            httpContextBase.Response.Buffer = true;
            httpContextBase.Response.Clear();

            httpContextBase.Response.ContentType = "text/xml";

            using (System.IO.StringWriter writer = new System.IO.StringWriter())
            {
                var data = new Core.CommerceMLConverter().Convert(this.Invoices);

                System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(CommerceML.Version205.КоммерческаяИнформация));
                xml.Serialize(writer, data);
                httpContextBase.Response.Write(writer);
            }
        }
    }
}